package com.beilang.hotline.person.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.person.model.PersonModel;
import com.beilang.hotline.person.to.SearchTO;
@SuppressWarnings("unchecked")
public class PersonDAO extends PaginationBaseDAO{
	public PersonModel createPerson(PersonModel personModel) {
        personModel.setDeleteInd("N");
        return (PersonModel)this.save(personModel);
	}
	
	public PersonModel savePerson(PersonModel personModel) {
		return (PersonModel)this.save(personModel);
	}
	
	public void deletePerson(Long personId){
		PersonModel person= this.getPerson(personId);
        person.setDeleteInd("Y");
        this.save(person);
	}
	
	public PersonModel getPerson(Long personId){
		PersonModel person= (PersonModel)this.get(PersonModel.class, personId);
        if(person!=null){
            person.setTeleBackIndStr(person.getTeleBackIndStr());
            person.setSurveyIndStr(person.getSurveyIndStr());
        }
        return person;
    }
	

	public Integer searchCount(SearchTO searchTO) {                  
        Map map = new HashMap();
        map=constructWhereClause(map,searchTO);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchPersonCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
    	Map map = new HashMap();
        map=constructWhereClause(map,searchTO);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchPersonList", map);
    }
    
    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("companyName")) return "company_name";
    	if(dataField.equalsIgnoreCase("personName")) return "name";
    	if(dataField.equalsIgnoreCase("sexDesc")) return "sex";
    	if(dataField.equalsIgnoreCase("fullTel")) return "telno";
    	if(dataField.equalsIgnoreCase("handPhone")) return "handphone";
    	if(dataField.equalsIgnoreCase("email")) return "email";    	
    	return dataField;
    }

      public List searchCompanyPersonList(SearchTO searchTO) {
    	Map map = new HashMap();
        map=constructWhereClause(map,searchTO);
        return getSqlMapClientTemplate().queryForList("searchCompanyPersonList", map);
    }

    private Map constructWhereClause(Map map, SearchTO searchTO){
        String customArea=StringUtil.convertEmpty2Null(searchTO.getCustomArea());
        if(customArea!=null){
        	map.put("customArea", customArea);
        }
        map.put("companyId", searchTO.getCompanyId());
        String name=StringUtil.convertEmpty2Null(searchTO.getPersonName());
        if(name!=null){
        	name="%"+name+"%";
        	map.put("personName", name);
        }
        String email=StringUtil.convertEmpty2Null(searchTO.getEmail());
        if(email!=null){
        	email="%"+email+"%";
        	map.put("email", email);
        }
        String handPhone=StringUtil.convertEmpty2Null(searchTO.getHandPhone());
        if(handPhone!=null){
        	handPhone="%"+handPhone+"%";
        	map.put("handPhone", handPhone);
        }
        String telExt=StringUtil.convertEmpty2Null(searchTO.getTelExt());
        if(telExt!=null){
        	telExt="%"+telExt+"%";
        	map.put("telExt", telExt);
        }
        String telNo=StringUtil.convertEmpty2Null(searchTO.getTelNo());
        if(telNo!=null){
        	telNo="%"+telNo+"%";
        	map.put("telNo", telNo);
        }
        List list=searchTO.getAccessSystemList();
        if(list!=null&&list.size()>0){
            StringBuffer sysWhereClause=new StringBuffer("(");
            for (Object aList : list) {
                String s=(String)aList;
                if(!StringUtil.isNullOrBlank(s)){
                    if(!sysWhereClause.toString().equalsIgnoreCase("(")){
                    sysWhereClause.append(" or ");
                    }
                    sysWhereClause.append("(");
                    sysWhereClause=sysWhereClause.append("access_system = '");
                    sysWhereClause=sysWhereClause.append(s);
                    sysWhereClause=sysWhereClause.append("' or ");
                    sysWhereClause=sysWhereClause.append("access_system like '");
                    sysWhereClause=sysWhereClause.append(s).append("|").append("%'");
                    sysWhereClause=sysWhereClause.append(" or ");
                    sysWhereClause=sysWhereClause.append("access_system like '%");
                    sysWhereClause=sysWhereClause.append("|").append(s).append("|%'");
                    sysWhereClause=sysWhereClause.append(" or ");
                    sysWhereClause=sysWhereClause.append("access_system like '%");
                    sysWhereClause=sysWhereClause.append("|").append(s).append("'");
                    sysWhereClause=sysWhereClause.append(")");
                }
            }
            String sys=sysWhereClause.toString();
            sys=sys+")";
            if(!"()".equalsIgnoreCase(sys)){
                map.put("sys",sys);
            }
        }
        return map;
    }

    public void deleteAllCompanyPersons(Long companyId){
        Map map = new HashMap();
        map.put("companyId", companyId);
        getSqlMapClientTemplate().update("deleteAllCompanyPersons",map);
    }

    public List getPersonList(String personName, String telNo, String handPhone){
        Map map = new HashMap();
        map.put("personName", StringUtil.convertEmpty2Null(personName));
        map.put("telNo", StringUtil.convertEmpty2Null(telNo));
        map.put("handPhone",StringUtil.convertEmpty2Null( handPhone));
        return getSqlMapClientTemplate().queryForList("getPersonList",map);
    }

}
